package com.bdqn.carrental.controller;

import com.bdqn.carrental.pojo.UserEntity;
import com.bdqn.carrental.dao.UserJPA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * ========================
 * Created with IntelliJ IDEA.
 * User：LYQ
 * Date：2017/4/7
 * Time：21:17
 * 码云：http://git.oschina.net/jnyqy
 * ========================
 */
@Controller //证明是controller层并且返回json
@RequestMapping(value = "/user")
public class LoginController {

    @Autowired
    private UserJPA userJPA;

    @RequestMapping(value = "/login" ,method = RequestMethod.POST)
    @ResponseBody
    public Object login(UserEntity user, HttpServletRequest request)
    {
        //登录成功
        boolean flag = true;
        String result = "";
        //根据用户名查询用户是否存在
        Optional<UserEntity> userEntity = userJPA.findOne(new Specification<UserEntity>() {
            @Override
            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                criteriaQuery.where(criteriaBuilder.equal(root.get("username"), user.getUsername()));
                return null;
            }
        });

        //用户不存在
        if(!userEntity.isPresent()){
            flag = false;
            return 1;
        }
        //密码错误
        else if(!userEntity.get().getUserpwd().equals(user.getUserpwd())){
            flag = false;
            return 2;
        }
        //登录成功
        if(flag){
            //将用户写入session
            request.getSession().setAttribute("_session_user",userEntity.get());
        }

        return 0;
    }
    /**
     * 分页查询
     *
     * @return
     */
   /* @RequestMapping("/categorylist")
    public ModelAndView categorylist(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                     @RequestParam(value = "limit", defaultValue = "2") Integer limit) {
        start = start < 0 ? 0 : start;
        Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "username");
        Pageable pageable = new PageRequest(start, limit, sort);
        Page<UserEntity> page = userJPA.findAll(pageable);
        ModelAndView mav = new ModelAndView("categorylist");
        mav.addObject("page", page);
        return mav;
    }*/
    @RequestMapping(value ="/userpage")
    public List<UserEntity> userPage(int page){
        UserEntity user=new UserEntity();
        user.setSize(2);
        user.setPage(page);
        //创建分页对象
        PageRequest  pageRequest=new PageRequest(user.getPage()-1,user.getSize());
       //执行分页查询
        return  userJPA.findAll(pageRequest).getContent();
    }

}
